package com.nice.main;

import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.nice.controller.H2Dao;
import com.nice.controller.MessageDispatch;
import com.nice.message.MessageCodec;
import com.nice.service.Service;
import com.nice.tcp.TCPService;
import com.nice.web.WebService;

public class Bootstart {

	private static final Bootstart INSTANCE = new Bootstart();
	private static Logger log = Logger.getLogger(MessageCodec.class);
	private Map<String, Service> service = new ConcurrentHashMap<String, Service>();

	private Bootstart() {
	}

	public static Bootstart getInstance() {
		return INSTANCE;
	}

	public void addService(String name, Service ser) {
		service.put(name, ser);
	}

	public Service getService(String name) {
		return service.get(name);
	}

	public static void main(String[] args) throws Exception {
		Bootstart bt = Bootstart.getInstance();
		bt.addService("dao", new H2Dao());
		bt.addService("web", new WebService());
		bt.addService("tcp", new TCPService(2001));
		bt.addService("dispatch", MessageDispatch.getInstance());
		for (Service ser : bt.service.values()) {
			ser.start(bt);
			log.info(ser + " is running");
		}
		log.info("all service running");
		Scanner sc = new Scanner(System.in);
		String line = "";
		while (!line.equals("q")) {
			line = sc.next();
			System.out.println("enter q to exit");
		}
		sc.close();
		for (Service ser : bt.service.values()) {
			ser.stop(bt);
			log.info(ser + " is stop");
		}
		log.info(" service stop");
	}
}